package main

import (
	"fmt"
	"path/filepath"
	"os"
	"log"
	"bytes"
)

func main() {
	//fmt.Println("hello")
	//dest := "./test.zip"
	//d, _ := os.Create(dest)
	//defer d.Close()
	//buf := new(bytes.Buffer)
	//w := zip.NewWriter(buf)
	//defer w.Close()
	//if err != nil {
	//	panic(err)
	//}
	//err = compress(src, "", w)
	//if err != nil {
	//	panic(err)
	//}
	//fmt.Println(buf.Len())
	//buf.WriteTo(d)
	fmt.Println(filepath.Abs("./"))

	//err := Compress("./encrypt", "./encrypted.zip")
	//if err != nil {
	//	log.Fatal(err)
	//}

	//pass64 := base64.StdEncoding.EncodeToString(xpass)
	//fmt.Printf("加密后: %s\n", pass64)
	//
	//bytesPass, err := base64.StdEncoding.DecodeString(pass64)
	//if err != nil {
	//	fmt.Println(err)
	//	return
	//}
	//
	//tpass, err := AesDecrypt(bytesPass, aesKey)
	//if err != nil {
	//	fmt.Println(err)
	//	return
	//}
	//fmt.Printf("解密后: %s\n", tpass)
	decrypt()
}

func encrypt() {
	zipBytes, err := CompressToBytes("./encrypt")

	var aesKey = []byte("12345678901234561234567890123456")
	pass := zipBytes
	xpass, err := AesEncrypt(pass, aesKey)
	if err != nil {
		fmt.Println(err)
		return
	}

	fd, err := os.Create("./encrypted.aes")
	if err != nil {
		log.Fatal(err)
	}
	fd.Write(xpass)
	fd.Close()
}

func decrypt() {
	var aesKey = []byte("12345678901234561234567890123456")
	fd, err := os.Open("./encrypted.aes")
	if err != nil {
		log.Fatal(err)
	}
	buff := new(bytes.Buffer)
	_, err = buff.ReadFrom(fd)
	fd.Close()

	if err != nil {
		log.Fatal(err)
	}

	tpass, err := AesDecrypt(buff.Bytes(), aesKey)
	if err != nil {
		fmt.Println(err)
		return
	}
	//fmt.Printf("解密后: %s\n", tpass)

	fd, err = os.Create("./decrypted.zip")
	if err != nil {
		log.Fatal(err)
	}
	fd.Write(tpass)
	fd.Close()
}
